﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Chromium;
using Chromium.WebBrowser;
using Kugar.UI.RazorUI.CefEngine;
using NetDimension.NanUI;

namespace Kugar.UI.RazorUI
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new RazorUIForm(""));

            if (Bootstrap.Load(setting => { setting.Locale = "zh-CN"; }, line =>
            {
                line.AppendSwitch("disabled-web-security");
            }))
            {
                RegisterRazorResources(AppDomain.CurrentDomain.BaseDirectory + "/Assets");

                //Application.Run(new Form1());

                //Application.Exit();
            }
        }

        public static void RegisterRazorResources(string path, string domainName = "assets.razor.local")
        {
            var factory = new CfxSchemeHandlerFactory();


            factory.Create += (_, args) =>
            {
                
                if (args.SchemeName == "http" && args.Browser != null)
                {
                    var wb = BrowserCore.GetBrowser(args.Browser.Identifier);
                    var handler = new RazorResourceHandler(path, wb,args.Frame, domainName);
                    args.SetReturnValue(handler);
                }
            };

            Bootstrap.RegisterCustomScheme("http", domainName, factory);

        }
    }
}
